package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsHobby;
import yx.vo.DatagridVO;
import yx.vo.HobbyVO;

@Service("hobbyService")
public class HobbyServiceImpl implements HobbyServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(HobbyServiceImpl.class);

	private BaseDaoInt<BsHobby> hobbyDao;

	public BaseDaoInt<BsHobby> getHobbyDao() {
		return hobbyDao;
	}

	@Autowired
	public void setHobbyDao(BaseDaoInt<BsHobby> hobbyDao) {
		this.hobbyDao = hobbyDao;
	}

	@Override
	public HobbyVO save(HobbyVO hobby) {
		BsHobby t = new BsHobby();
		BeanUtils.copyProperties(hobby, t);

		/**
		 * private Integer id; private Integer studentId; private String corporationName; private String title; private String position; private String headFlag; private Date startDate; private Date endDate; private String region;
		 */

		hobbyDao.save(t);
		BeanUtils.copyProperties(t, hobby);
		return hobby;
	}

	@Override
	public DatagridVO datagrid(HobbyVO hobby) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsHobby t";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(hobby, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(hobby, hql);
		List<BsHobby> l = hobbyDao.find(hql, params, hobby.getPage(), hobby.getRows());
		List<HobbyVO> nl = new ArrayList<HobbyVO>();
		changeModel(l, nl);
		dg.setTotal(hobbyDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsHobby> l, List<HobbyVO> nl) {
		if (l != null && l.size() > 0) {
			for (BsHobby t : l) {
				HobbyVO u = new HobbyVO();
				BeanUtils.copyProperties(t, u);
				nl.add(u);
			}
		}
	}

	private String addOrder(HobbyVO hobby, String hql) {
		if (hobby.getSort() != null || hobby.getOrder() != null) {
			hql += " order by " + hobby.getSort() + " " + hobby.getOrder();
		}
		return hql;
	}

	private String addWhere(HobbyVO hobby, String hql, Map<String, Object> params) {
		if (hobby.getStudentId() != null) {
			hql += " where t.studentId = :studentId";
			params.put("studentId", hobby.getStudentId());
		}
		return hql;
	}

	@Override
	public void remove(String ids) {
		// for(String id : ids.split(",")){
		// SmUser u = userDao.get(SmUser.class, id);
		// if(u != null){
		// userDao.delete(u);
		// }
		// }
		String[] nids = ids.split(",");
		String hql = "delete BsHobby t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		hobbyDao.executeHql(hql);
	}

	@Override
	public HobbyVO edit(HobbyVO hobby) {
		BsHobby t = hobbyDao.get(BsHobby.class, hobby.getId());
		BeanUtils.copyProperties(hobby, t);
		return hobby;
	}

}
